<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-的文件系统和云存储功能集成" tabindex="-1"><a class="header-anchor" href="#laravel-的文件系统和云存储功能集成"><span>Laravel 的文件系统和云存储功能集成</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#configuration">配置</a>
<ul>
<li><a href="#the-public-disk">公共磁盘</a></li>
<li><a href="#the-local-driver">本地驱动</a></li>
<li><a href="#driver-prerequisites">驱动之前</a></li>
</ul>
</li>
<li><a href="#obtaining-disk-instances">获取磁盘实例</a></li>
<li><a href="#retrieving-files">检索文件</a>
<ul>
<li><a href="#file-urls">文件 URLs</a></li>
<li><a href="#file-metadata">文件元数据</a></li>
</ul>
</li>
<li><a href="#storing-files">保存文件</a>
<ul>
<li><a href="#file-uploads">文件上传</a></li>
<li><a href="#file-visibility">文件可见性</a></li>
</ul>
</li>
<li><a href="#deleting-files">删除文件</a></li>
<li><a href="#directories">目录</a></li>
<li><a href="#custom-filesystems">自定义文件系统</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Laravel 提供了一个强大的文件系统抽象，这得益于 Frank de Jonge 强大的 <a href="https://github.com/thephpleague/flysystem" target="_blank" rel="noopener noreferrer">Flysystem</a> 扩展包。Laravel 文件系统集成为使用本地文件系统、Amazon S3 和 Rackspace 云存储提供了简单易用的驱动程序。更棒的是，由于每个系统的 API 保持不变，所以在这些存储选项之间切换是非常简单的。</p>
<h2 id="配置" tabindex="-1"><a class="header-anchor" href="#配置"><span>配置</span></a></h2>
<p>文件系统的配置文件位于 <code v-pre>config/filesystems.php</code>。在这个文件中你可以配置所有「磁盘」。每个磁盘代表特定的存储驱动及存储位置。每种支持的驱动程序的示例配置都包含在配置文件中。因此，只需要修改配置即可反映你的存储偏好和凭据。</p>
<p>当然，你可以根据需要配置多个磁盘，甚至你还可以使多个磁盘共用同一个驱动。</p>
<h3 id="公共磁盘" tabindex="-1"><a class="header-anchor" href="#公共磁盘"><span>公共磁盘</span></a></h3>
<p><code v-pre>public</code> 磁盘适用于要公开访问的文件。默认情况下， <code v-pre>public</code> 磁盘使用 <code v-pre>local</code> 驱动，并且将这些文件存储在 <code v-pre>storage/app/public</code> 目录下。为了使它们能通过网络访问，你需要创建 <code v-pre>public/storage</code> 到 <code v-pre>storage/app/public</code> 的符号链接。这种方式能把可公开访问文件都保留在同一个目录下，以便在使用零停机时间部署系统如<a href="https://envoyer.io/" target="_blank" rel="noopener noreferrer">Envoyer</a> 的时候，就可以轻松地在不同的部署之间共享这些文件。</p>
<p>你可以使用 Artisan 命令 <code v-pre>storage:link</code> 来创建符号链接：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan storage<span class="token punctuation">:</span>link</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>当然，一旦一个文件被存储并且已经创建了符号链接，你就可以使用辅助函数 <code v-pre>asset</code> 来创建文件的 URL：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">asset</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'storage/file.txt'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="本地驱动" tabindex="-1"><a class="header-anchor" href="#本地驱动"><span>本地驱动</span></a></h3>
<p>使用 <code v-pre>local</code> 驱动时，所有文件操作都你在配置文件中定义的 <code v-pre>root</code> 目录相关。该目录的默认值是 <code v-pre>storage/app</code>。因此，以下方法会把文件存储在 <code v-pre>storage/app/file.txt</code> 中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">disk</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'local'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.txt'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Contents'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="驱动之前" tabindex="-1"><a class="header-anchor" href="#驱动之前"><span>驱动之前</span></a></h3>
<h4 id="composer-包" tabindex="-1"><a class="header-anchor" href="#composer-包"><span>Composer 包</span></a></h4>
<p>在使用 S3 或 Rackspace 的驱动之前，你需要通过 Composer 安装相应的软件包：</p>
<ul>
<li>Amazon S3: <code v-pre>league/flysystem-aws-s3-v3 ~1.0</code></li>
<li>Rackspace: <code v-pre>league/flysystem-rackspace ~1.0</code></li>
</ul>
<h4 id="s3-驱动配置" tabindex="-1"><a class="header-anchor" href="#s3-驱动配置"><span>S3 驱动配置</span></a></h4>
<p>S3 驱动的配置信息位于 <code v-pre>config/filesystems.php</code> 配置文件中。该文件包含 S3 驱动程序的配置数组示例。你可根据自己的 S3 配置和凭证自由修改该数组。</p>
<h4 id="ftp-驱动配置" tabindex="-1"><a class="header-anchor" href="#ftp-驱动配置"><span>FTP 驱动配置</span></a></h4>
<p>Laravel 的文件系统集成能很好的支持 FTP，不过 FTP 的配置示例并没有被包含在框架默认的 <code v-pre>filesystems.php</code> 文件中。需要的话可以使用下面的示例配置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'ftp'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'driver'</span>   <span class="token operator">=></span> <span class="token string single-quoted-string">'ftp'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'host'</span>     <span class="token operator">=></span> <span class="token string single-quoted-string">'ftp.example.com'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your-username'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your-password'</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Optional FTP Settings...</span></span>
<span class="line">    <span class="token comment">// 'port'     => 21,</span></span>
<span class="line">    <span class="token comment">// 'root'     => '',</span></span>
<span class="line">    <span class="token comment">// 'passive'  => true,</span></span>
<span class="line">    <span class="token comment">// 'ssl'      => true,</span></span>
<span class="line">    <span class="token comment">// 'timeout'  => 30,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="rackspace-驱动配置" tabindex="-1"><a class="header-anchor" href="#rackspace-驱动配置"><span>Rackspace 驱动配置</span></a></h4>
<p>Laravel 的文件系统集成也能很好的支持 Rackspace，不过 Rackspace 的配置示例也没被包含在框架默认的 <code v-pre>filesystems.php</code> 文件中。需要的话可以使用下面的示例配置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'rackspace'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'driver'</span>    <span class="token operator">=></span> <span class="token string single-quoted-string">'rackspace'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'username'</span>  <span class="token operator">=></span> <span class="token string single-quoted-string">'your-username'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'key'</span>       <span class="token operator">=></span> <span class="token string single-quoted-string">'your-key'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'container'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your-container'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'endpoint'</span>  <span class="token operator">=></span> <span class="token string single-quoted-string">'https://identity.api.rackspacecloud.com/v2.0/'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'region'</span>    <span class="token operator">=></span> <span class="token string single-quoted-string">'IAD'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'url_type'</span>  <span class="token operator">=></span> <span class="token string single-quoted-string">'publicURL'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="获取磁盘实例" tabindex="-1"><a class="header-anchor" href="#获取磁盘实例"><span>获取磁盘实例</span></a></h2>
<p><code v-pre>Storage</code> facade 用于和所有已配置的磁盘进行交互。例如，你可以使用 facade 的 <code v-pre>put</code> 方法将头像保存到默认磁盘上。调 <code v-pre>Storage</code> facade 的方法前若未先调用 <code v-pre>disk</code> 方法，此方法会被自动传递给默认磁盘。如果没有先调用 <code v-pre>disk</code> 方法就调用了 <code v-pre>Storage</code> facade 上的方法，那调用的方法会自动被传递到默认磁盘：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatars/1'</span><span class="token punctuation">,</span> <span class="token variable">$fileContents</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果应用程序和多个磁盘交互，则可以使用 <code v-pre>Storage</code> facade 上的 <code v-pre>disk</code> 方法来处理特定磁盘上的文件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">disk</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'s3'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatars/1'</span><span class="token punctuation">,</span> <span class="token variable">$fileContents</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="检索文件" tabindex="-1"><a class="header-anchor" href="#检索文件"><span>检索文件</span></a></h2>
<p><code v-pre>get</code> 方法可以用于检索文件的内容，此方法返回该文件的原始字符串内容。 切记，所有文件路径的指定都应该相对于为磁盘配置的 <code v-pre>root</code> 目录：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$contents</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>exists</code> 方法可以用来判断磁盘上是否存在指定的文件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$exists</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">disk</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'s3'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="文件-urls" tabindex="-1"><a class="header-anchor" href="#文件-urls"><span>文件 URLs</span></a></h3>
<p>当使用 <code v-pre>local</code> 或者 <code v-pre>s3</code> 驱动时，你可以使用 <code v-pre>url</code> 方法来获取给定文件的 URL。如果你使用的是 <code v-pre>local</code> 驱动，一般只是在给定的路径前面加上 <code v-pre>/storage</code> 并返回一个相对的 URL 到那个文件。如果使用的是 <code v-pre>s3</code> 驱动，会返回完整的远程 URL：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file1.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>{note} 切记，如果使用的是 <code v-pre>local</code> 驱动，则所有想被公开访问的文件都应该放在 <code v-pre>storage/app/public</code> 目录下。此外，你应该在 <code v-pre>public/storage</code> <a href="#the-public-disk">创建一个符号链接</a> 来指向 <code v-pre>storage/app/public</code> 目录。</p>
</blockquote>
<h4 id="临时-urls" tabindex="-1"><a class="header-anchor" href="#临时-urls"><span>临时 URLs</span></a></h4>
<p>对于使用 <code v-pre>s3</code> 驱动来存储的文件，可以使用 <code v-pre>temporaryUrl</code> 方法创建给定文件的临时 URL 。这个方法接收路径和 <code v-pre>DateTime</code> 实例来指定 URL 何时过期：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">temporaryUrl</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'file1.jpg'</span><span class="token punctuation">,</span> <span class="token class-name static-context">Carbon</span><span class="token operator">::</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addMinutes</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义本地-url-主机" tabindex="-1"><a class="header-anchor" href="#自定义本地-url-主机"><span>自定义本地 URL 主机</span></a></h4>
<p>如果要使用 <code v-pre>local</code> 驱动为存储在磁盘上的文件预定义主机，可以向磁盘配置数组添加一个 <code v-pre>url</code> 选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'public'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'driver'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'local'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'root'</span> <span class="token operator">=></span> <span class="token function">storage_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app/public'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'APP_URL'</span><span class="token punctuation">)</span><span class="token operator">.</span><span class="token string single-quoted-string">'/storage'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'visibility'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'public'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="文件元数据" tabindex="-1"><a class="header-anchor" href="#文件元数据"><span>文件元数据</span></a></h3>
<p>除了读写文件，Laravel 还可以提供有关文件本身的信息。例如，<code v-pre>size</code> 方法可用来获取文件的大小（以字节为单位）：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$size</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file1.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>lastModified</code> 方法返回最后一次文件被修改的 UNIX 时间戳：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$time</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">lastModified</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file1.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="保存文件" tabindex="-1"><a class="header-anchor" href="#保存文件"><span>保存文件</span></a></h2>
<p><code v-pre>put</code> 方法可用于将原始文件内容保存到磁盘上。你也可以传递 PHP 的 <code v-pre>resource</code> 给 put 方法，它将使用文件系统下的底层流支持。强烈建议在处理大文件时使用流：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">,</span> <span class="token variable">$contents</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">,</span> <span class="token variable">$resource</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自动流式传输" tabindex="-1"><a class="header-anchor" href="#自动流式传输"><span>自动流式传输</span></a></h4>
<p>如果你想 Laravel 自动管理将给定文件流式传输到你想要的存储位置，你可以使用 <code v-pre>putFile</code> 或 <code v-pre>putFileAs</code> 方法。这个方法接受 <code v-pre>Illuminate\HTTP\File</code> 或 <code v-pre>Illuminate\HTTP\UploadedFile</code> 实例，并自动将文件流式传输到你想要的位置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>File</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 自动为文件名生成唯一的 ID...</span></span>
<span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">putFile</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photos'</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/photo'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 手动指定文件名...</span></span>
<span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">putFileAs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photos'</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/photo'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'photo.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>关于 <code v-pre>putFile</code> 方法有些重要的事情要注意。请注意，我们只指定一个目录名，而不是文件名。默认情况下，<code v-pre>putFile</code> 方法将生成唯一的 ID 作为文件名。该文件的路径将被 <code v-pre>putFile</code> 方法返回，因此可以将路径（包括生成的文件名）存储在数据库中。</p>
<p><code v-pre>putFile</code> 和 <code v-pre>putFileAs</code> 方法也接受一个参数来指定存储文件的「可见性」。如果你将文件存储在诸如 S3 的云盘上，并且该文件可以公开访问，这是特别有用的：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">putFile</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photos'</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/photo'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'public'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="前置-追加" tabindex="-1"><a class="header-anchor" href="#前置-追加"><span>前置&amp;追加</span></a></h4>
<p><code v-pre>prepend</code> 及 <code v-pre>append</code> 方法允许你写入内容到文件的开头或结尾：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">prepend</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.log'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Prepended Text'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.log'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Appended Text'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="复制-移动" tabindex="-1"><a class="header-anchor" href="#复制-移动"><span>复制 &amp; 移动</span></a></h4>
<p><code v-pre>copy</code> 方法可用于将现有文件复制到磁盘的新位置，而 <code v-pre>move</code> 方法可用于重命名或将现有文件移动到新位置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">copy</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'old/file1.jpg'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'new/file1.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">move</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'old/file1.jpg'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'new/file1.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="文件上传" tabindex="-1"><a class="header-anchor" href="#文件上传"><span>文件上传</span></a></h3>
<p>在 Web 应用中，存储文件最常见的例子之一就是存储用户上传的文件，如个人资料图片、照片和文档。 Laravel 可以使用文件上传实例的 <code v-pre>store</code> 方法来存储上传的文件。只需使用你想存储上传文件的路径调用 <code v-pre>store</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">UserAvatarController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 更新用户头像。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name">Request</span>  <span class="token parameter">$request</span></span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name">Response</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">update</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$path</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">store</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatars'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$path</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>关于这个例有些注意事项。我们只指定目录名，而不是文件名。默认情况下，<code v-pre>store</code> 方法将生成唯一的 ID 来作为文件名。该文件的路径将被 <code v-pre>store</code> 方法返回，因此你可以在数据库中存储路径及生成的文件名。</p>
<p>你也可以调用 <code v-pre>Storage</code> facade 的 <code v-pre>putFile</code> 方法来执行和上面例子相同的文件操作：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">putFile</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatars'</span><span class="token punctuation">,</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatar'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="指定文件名" tabindex="-1"><a class="header-anchor" href="#指定文件名"><span>指定文件名</span></a></h4>
<p>如果你不喜欢自动生成的文件名，你可以使用 <code v-pre>storeAs</code> 方法，它接收的路径、文件名、磁盘（可选的）作为它的参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">storeAs</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'avatars'</span><span class="token punctuation">,</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">id</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当然，你也可以使用 <code v-pre>Storage</code> facade 的 <code v-pre>putFileAs</code> 方法，和上面例子的文件操作有相同效果：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">putFileAs</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'avatars'</span><span class="token punctuation">,</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatar'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">id</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="指定磁盘" tabindex="-1"><a class="header-anchor" href="#指定磁盘"><span>指定磁盘</span></a></h4>
<p>默认情况下，此方法将使用默认的磁盘。如果你想指定其他磁盘，就将磁盘名称作为第二个参数传递给 <code v-pre>store</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">store</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'avatars/'</span><span class="token operator">.</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'s3'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="文件可见性" tabindex="-1"><a class="header-anchor" href="#文件可见性"><span>文件可见性</span></a></h3>
<p>在 Laravel 的文件系统集成里，「可见性」 是跨多个平台的文件权限的抽象。文件可能被声明为 <code v-pre>public</code> 或 <code v-pre>private</code> 。当一个文件声明为 <code v-pre>public</code> 时，就意味着文件通常可以被其他人访问。例如，使用 S3 驱动时，你可检索 <code v-pre>public</code> 文件的 URL。</p>
<p>你可通过 <code v-pre>put</code> 方法在设置文件时指定它的可见性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">,</span> <span class="token variable">$contents</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'public'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果文件已经被保存，可以通过 <code v-pre>getVisibility</code> 和 <code v-pre>setVisibility</code> 方法检索和设置其可见性。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$visibility</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">getVisibility</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">setVisibility</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'public'</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="删除文件" tabindex="-1"><a class="header-anchor" href="#删除文件"><span>删除文件</span></a></h2>
<p><code v-pre>delete</code> 方法接受文件名或文件名数组参数来删除磁盘中相应的文件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'file1.jpg'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'file2.jpg'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="目录" tabindex="-1"><a class="header-anchor" href="#目录"><span>目录</span></a></h2>
<h4 id="获取目录中的所有文件" tabindex="-1"><a class="header-anchor" href="#获取目录中的所有文件"><span>获取目录中的所有文件</span></a></h4>
<p><code v-pre>files</code> 方法返回给定目录下的所有文件的数组。如果你想检索包含所有子目录在内的给定目录中的所有文件的列表，可以使用 <code v-pre>allFiles</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$files</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">files</span><span class="token punctuation">(</span><span class="token variable">$directory</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$files</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">allFiles</span><span class="token punctuation">(</span><span class="token variable">$directory</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="获取目录内所有目录" tabindex="-1"><a class="header-anchor" href="#获取目录内所有目录"><span>获取目录内所有目录</span></a></h4>
<p><code v-pre>directories</code> 方法返回给定目录下的所有目录的数组。另外，你可以使用 <code v-pre>allDirectories</code> 方法获取给定目录及其所有子目录中的所有目录的列表：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$directories</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">directories</span><span class="token punctuation">(</span><span class="token variable">$directory</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 递归...</span></span>
<span class="line"><span class="token variable">$directories</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">allDirectories</span><span class="token punctuation">(</span><span class="token variable">$directory</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="创建目录" tabindex="-1"><a class="header-anchor" href="#创建目录"><span>创建目录</span></a></h4>
<p><code v-pre>makeDirectory</code> 方法将创建给定的目录，包括任何所需的子目录：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">makeDirectory</span><span class="token punctuation">(</span><span class="token variable">$directory</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="删除目录" tabindex="-1"><a class="header-anchor" href="#删除目录"><span>删除目录</span></a></h4>
<p>最后，<code v-pre>deleteDirectory</code> 方法可用于删除目录及其所有文件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">deleteDirectory</span><span class="token punctuation">(</span><span class="token variable">$directory</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="自定义文件系统" tabindex="-1"><a class="header-anchor" href="#自定义文件系统"><span>自定义文件系统</span></a></h2>
<p>Laravel 的文件系统集成提供一系列开箱即用的驱动支持；然而文件系统不仅限于此，它还拥有其它存储系统的适配器。如果你想在 Laravel 的应用中使用额外的存储适配器，可以创建自定义驱动。</p>
<p>为了设置自定义文件系统，你需要一个文件系统适配器。让我们添加一个社区维护的 Dropbox 适配器到我们的项目中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">require</span> spatie<span class="token operator">/</span>flysystem<span class="token operator">-</span>dropbox</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>接下来，你需要创建一个<a href="https://learnku.com/docs/laravel/5.5/providers" target="_blank" rel="noopener noreferrer">服务提供器</a>，如 DropboxServiceProvider。并在该提供器的 <code v-pre>boot</code> 方法使用 <code v-pre>Storage</code> facade 的 <code v-pre>extend</code> 方法自定义你的驱动程序。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Providers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Storage</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">League<span class="token punctuation">\</span>Flysystem<span class="token punctuation">\</span>Filesystem</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Spatie<span class="token punctuation">\</span>Dropbox<span class="token punctuation">\</span>Client</span> <span class="token keyword">as</span> DropboxClient<span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>ServiceProvider</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Spatie<span class="token punctuation">\</span>FlysystemDropbox<span class="token punctuation">\</span>DropboxAdapter</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">DropboxServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 执行注册后引导服务。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">extend</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'dropbox'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$app</span><span class="token punctuation">,</span> <span class="token variable">$config</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DropboxClient</span><span class="token punctuation">(</span></span>
<span class="line">                <span class="token variable">$config</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'authorizationToken'</span><span class="token punctuation">]</span></span>
<span class="line">            <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">            <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Filesystem</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">DropboxAdapter</span><span class="token punctuation">(</span><span class="token variable">$client</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 在容器中注册绑定。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">register</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">//</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>extend</code> 方法的第一个参数是驱动程序的名称，第二个参数是接收 <code v-pre>$app</code> 及 <code v-pre>$config</code> 变量的闭包。该解析闭包必须返回 <code v-pre>League\Flysystem\Filesystem</code> 的实例。<code v-pre>$config</code> 变量包含了特定磁盘在 <code v-pre>config/filesystems.php</code> 中定义的值。</p>
<p>一旦通过创建服务提供器注册扩展名后，你就可以在 <code v-pre>config/filesystem.php</code> 配置文件中使用 <code v-pre>dropbox</code> 驱动程序。</p>
<h2 id="译者署名" tabindex="-1"><a class="header-anchor" href="#译者署名"><span>译者署名</span></a></h2>
<table>
<thead>
<tr>
<th>用户名</th>
<th>头像</th>
<th>职能</th>
<th>签名</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://segmentfault.com/blog/godruoyi" target="_blank" rel="noopener noreferrer">@Godruoyi</a></td>
<td><img src="https://cdn.learnku.com/uploads/avatars/5359_1470894290.jpeg?imageView2/1/w/200/h/200" alt="200"></td>
<td>翻译</td>
<td>听大悲咒敲代码的二楞徐</td>
</tr>
<tr>
<td><a href="https://github.com/zuoRambo" target="_blank" rel="noopener noreferrer">@Rambone</a></td>
<td><img src="http://tva1.sinaimg.cn/crop.0.0.1002.1002.180/92d03bcdjw8f0asasf3m1j20ru0rvaeo.jpg" alt="92d03bcdjw8f0asasf3m1j20ru0rvaeo.jpg"></td>
<td>翻译</td>
<td>php,go求职 简历请发zuoxiaojie@lianjia.com</td>
</tr>
</tbody>
</table>
<hr>
<blockquote>
<p>{note} 欢迎任何形式的转载，但请务必注明出处，尊重他人劳动共创开源社区。</p>
<p>转载请注明：本文档由 Laravel China 社区 <a href="https://laravel-china.org/" target="_blank" rel="noopener noreferrer">laravel-china.org</a> 组织翻译，详见 <a href="https://learnku.com/laravel/t/5756/laravel-55-document-translation-call-come-and-join-the-translation" target="_blank" rel="noopener noreferrer">翻译召集帖</a>。</p>
<p>文档永久地址： <a href="https://learnku.com/docs/laravel" target="_blank" rel="noopener noreferrer">《Laravel 中文文档》</a></p>
</blockquote>
</div></template>


